import { AppCTX } from "../../types/koaExtend";
import { _pick, isUndefined, canUse } from "mmb";
import { createBrandApiMWRouter } from "../../middleware/brand-api/brandAdminChecker";
import {
  getActivityOfBAsFeature,
  checkActivityOfBParamFields,
  createActivityOfB,
  updateActivityOfB,
  pauseActivityOfB,
  resumeActivityOfB,
} from "../../service/activityService";
import {
  ActivityOfB,
  CreateActivityOfBParams,
  UpdateActivityOfBParams,
} from "../../service/declareTypes";
import ActivityType from "../../bg-enums/activityType";
import ActivitySaleMode from "../../bg-enums/activitySaleMode";
import ActivityProcessStatus from "../../bg-enums/activityProcessStatus";

const router = createBrandApiMWRouter();

// 获取签到1定义(品牌管理视角)
router.get("/brd/:brandId/checkIn1/def", async (ctx: AppCTX, next) => {
  const { brandId } = ctx.params;
  const { userId } = ctx;

  const activity = await getActivityOfBAsFeature(
    brandId,
    ActivityType.CHECK_IN__1
  );

  if (isUndefined(activity)) {
    console.log("NOT_FOUND")
    ctx.throw(404, "NOT_FOUND");
  }
  if (activity.brandId !== brandId) {
    ctx.throw(400, "no belong current brand");
  }

  ctx.body = {
    ...activity,
  };
});

// 更新签到1定义
router.put("/brd/:brandId/checkIn1/def", async (ctx: AppCTX, next) => {
  const { brandId } = ctx.params;
  const { userId } = ctx;
  const {
    activityId,
    name,
    intro,
    background,
    checkIn1,
    deliveryModes,
    tel,
    warehouseIds,
    activityProductWithSkus,
  } = ctx.request.body;
  let {
    begined,
    ended,
  } = ctx.request.body;

  const type = ActivityType.CHECK_IN__1;
  const saleMode = ActivitySaleMode.ONLY_BRAND_SELLING;
  const isUseWarehouseShippingFeeRule = false;
  const unifiedShippingFee = 1;
  const minimumPurchaseAmount = 0;

  const isCreate = !canUse(activityId);
  if (isUndefined(begined)) {
    begined = Date.now()
  }
  if (isUndefined(ended)) {
    ended = 32503651199000 // 2999-12-31 23:59:59
  }
  

  checkActivityOfBParamFields(
    {
      type,
      intro,
      begined,
      ended,
      checkIn1,
      saleMode,
      deliveryModes,
      isUseWarehouseShippingFeeRule,
      unifiedShippingFee,
      warehouseIds,
      activityProductWithSkus,
    },
    isCreate
  );

  let activity: ActivityOfB;

  if (isCreate) {
    activity = await createActivityOfB({
      brandId,
      pubUserId: userId,
      type,
      name,
      intro,
      begined,
      ended,
      background,
      checkIn1,
      saleMode,
      deliveryModes,
      isUseWarehouseShippingFeeRule,
      unifiedShippingFee,
      minimumPurchaseAmount,
      tel,
      // warehouses
      warehouseIds,
      // productWithSkus
      activityProductWithSkus,
    });
  } else {
    let p: UpdateActivityOfBParams = {
      type,
      activityId,
      name,
      intro,
      begined,
      ended,
      background,
      checkIn1,
      saleMode,
      deliveryModes,
      isUseWarehouseShippingFeeRule,
      unifiedShippingFee,
      minimumPurchaseAmount,
      tel,
      // warehouses
      warehouseIds,
      // productWithSkus
      activityProductWithSkus,
    };

    activity = await updateActivityOfB(p);
    if (
      !isCreate &&
      activity.processStatus !== ActivityProcessStatus.PROCESSING
    ) {
      await resumeActivityOfB({ activityId, brandId });
    }
  }

  ctx.body = {
    ...activity,
  };
});

// 关闭签到1
router.put("/brd/:brandId/checkIn1/pause", async (ctx: AppCTX, next) => {
  const { brandId } = ctx.params;
  const { userId } = ctx;
  const { activityId } = ctx.request.body;

  const processStatus = await pauseActivityOfB({ activityId, brandId });
  ctx.body = {
    activityId,
    processStatus,
  };
});

export default router;
